<template>
  <div class="login-body">
    <div class="login-container">
      <div class="head">
        <!-- <img
          class="logo"
          src="https://s.weituibao.com/1582958061265/mlogo.png"
        />       
        <div class="name">
          <div class="title">新蜂商城</div>
          <div class="tips">Vue3.0 后台管理系统</div>
        </div> -->
        <h1>WEB面试宝典</h1>
      </div>
      <el-form
        label-position="top"
        :rules="rules"
        :model="ruleForm"
        ref="loginForm"
        class="login-form"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            type="text"
            v-model.trim="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model.trim="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div style="color: #333">登录表示您已同意<a>《服务条款》</a></div>
          <el-button style="width: 100%" type="primary" @click="submitForm"
            >立即登录</el-button
          >
          <el-checkbox v-model="checked" @change="!checked"
            >下次自动登录</el-checkbox
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, toRefs, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { Login } from "../../api/login";
import { localSet } from "../../utils";
// 使用pinia
import { useUsersStore } from "../store/index";
// 返回pinia对象
const users = useUsersStore();

// console.log(users);

const { proxy } = getCurrentInstance();
const loginForm = ref(null);
// 定义响应式数据
let state = reactive({
  ruleForm: {
    username: "",
    password: "",
  },
  checked: true,
  rules: {
    username: [
      {
        required: "true", // 必填项
        message: "账户不能为空,且账户为6-15字母,数字,下划线", // 提示的信息
        trigger: "blur", // 触发的事件
        pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9]+$/, // 正则
      },
    ],
    password: [
      {
        required: "true", // 必填项
        message: "密码不能为空且密码为4-10数字", // 提示的信息
        trigger: "blur", // 触发的事件
        pattern: /^[0-9]{4,10}$/, // 正则
      },
    ],
  },
});

// 解构赋值 让reactive定义的数据继续有响应式
let { ruleForm, checked, rules } = toRefs(state);

// useRouter()  返回的是router对象
const router = useRouter();

// 提交表单
const submitForm = async () => {
  // 跳转
  // router.push("/home");
  loginForm.value.validate((valid) => {
    if (valid) {
      // 登录
      Login(proxy, state.ruleForm).then((res) => {
        // console.log(res);
        if (res.code === 200) {
          // 登录成功提示
          ElMessage({
            type: "success",
            message: res.msg,
          });
          // 接收用户名，进行保存
          localSet("username", res.username);
          // 接收token，进行保存
          // localSet("token", res.token);

          // 使用pinia
          users.saveUsers(res.username);
          // 跳转
          router.push("/home");
        } else if (res.code === 401) {
          // 登录失败提示
          ElMessage({
            type: "error",
            message: res.msg,
          });
        } else {
          // 登录失败提示
          ElMessage({
            type: "error",
            message: res.msg,
          });
        }
      });
    } else {
      ElMessage({
        type: "error",
        message: "登录失败！",
      });
      return false;
    }
  });
};

// 重置表单
// const resetForm = () => {
//   loginForm.value.resetFields();
// };
</script>

<style scoped>
.login-body {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #fff;
}
.login-container {
  width: 420px;
  height: 500px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
}
.head {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0 20px 0;
}
.head img {
  width: 100px;
  height: 100px;
  margin-right: 20px;
}
.head .title {
  font-size: 28px;
  color: #1baeae;
  font-weight: bold;
}
.head .tips {
  font-size: 12px;
  color: #999;
}
.login-form {
  width: 70%;
  margin: 0 auto;
}
</style>
<style>
.el-form--label-top .el-form-item__label {
  padding: 0;
}
.login-form .el-form-item {
  margin-bottom: 12px;
}
</style>
